package com.infomanage.service.study;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.StudyArmyDAO;
import com.infomanage.dao.StudyExerciseDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.StudyExercise;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.StudyExerciseItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class StudyExerciseService {

	public static StudyExerciseItem loadStudyExercise(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId= request.getParameter("itemId");
			StudyExerciseDAO dao = new StudyExerciseDAO();
			if(StringUtils.isEmpty(itemId)) return null;
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<StudyExerciseItem> loadStudyExerciseList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			StudyExerciseDAO dao = new StudyExerciseDAO();
			List<StudyExerciseItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveStudyExercise(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			StudyExercise study = new StudyExercise();
			StudyExerciseDAO dao = new StudyExerciseDAO();
			study.setCommanderId(Integer.parseInt(request.getParameter("commanderId")));
			study.setCorrective(request.getParameter("corrective"));
			study.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			study.setDetail(request.getParameter("detail"));
			study.setEvaluation(request.getParameter("evaluation"));
			study.setExecuteTime(request.getParameter("executeTime"));
			study.setExerciseTime(request.getParameter("exerciseTime"));
			study.setBeginPre(request.getParameter("beginPre"));
			study.setFinalPre(request.getParameter("finalPre"));
			study.setConsumePre(request.getParameter("consumePre"));
			study.setParticipants(request.getParameter("participants"));
			study.setPlace(request.getParameter("place"));
			study.setSubjects(request.getParameter("subjects"));
			study.setTimestamp(request.getParameter("timestamp"));
			dao.add(study);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeStudyExercise(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String studyId = request.getParameter("itemId");
			StudyExerciseDAO dao = new StudyExerciseDAO();
			dao.remove(StudyExercise.class, Integer.parseInt(studyId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject removeBatchStudyExercise(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			StudyExerciseDAO dao = new StudyExerciseDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(StudyExercise.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject modifyStudyExercise(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("itemId"));
			StudyExerciseDAO dao = new StudyExerciseDAO();
			StudyExercise study = dao.find(StudyExercise.class, planId);
			study.setCommanderId(Integer.parseInt(request.getParameter("commanderId")));
			study.setCorrective(request.getParameter("corrective"));
			study.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			study.setDetail(request.getParameter("detail"));
			study.setEvaluation(request.getParameter("evaluation"));
			study.setExecuteTime(request.getParameter("executeTime"));
			study.setExerciseTime(request.getParameter("exerciseTime"));
			study.setBeginPre(request.getParameter("beginPre"));
			study.setFinalPre(request.getParameter("finalPre"));
			study.setConsumePre(request.getParameter("consumePre"));
			study.setParticipants(request.getParameter("participants"));
			study.setPlace(request.getParameter("place"));
			study.setSubjects(request.getParameter("subjects"));
			study.setTimestamp(request.getParameter("timestamp"));
			dao.modify(study);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static List<StudyExerciseItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			StudyExerciseDAO dao = new StudyExerciseDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<StudyExerciseItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			StudyExerciseDAO dao = new StudyExerciseDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws Exception{
		request.setCharacterEncoding("UTF-8");
		String executeTime = request.getParameter("executeTime");
		String place = request.getParameter("place");
		String commanderId = request.getParameter("commanderId");
		SearchBean searchBean = new SearchBean();
		if(!StringUtil.isEmpty(executeTime)){
			FieldBean field = new FieldBean("executeTime", executeTime);
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(place)){
			FieldBean field = new FieldBean("place", place);
			field.setLike(true);
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(commanderId)){
			FieldBean field = new FieldBean("commanderId", commanderId);
			searchBean.add(field);
		}
		
		//设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if(user != null){
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
	return searchBean;
}
}
